package com.orangelife.mobile.individual.activity;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.sp.AppSp;
import com.common.sp.UserSp;
import com.common.update.UpdateBiz;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.SystemInfoUtil;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.app.service.MyService;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.common.view.NewConfirmDialog;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.shoppingcart.db.ShoppingCartDao;
import com.orangelife.mobile.util.CleanCacheUtils;
import com.orangelife.mobile.util.FileHelper;
import com.thirdsdk.jpush.JPushHelper;

import java.io.File;


/**
 * 设置
 *
 * @author lxj
 */

public class SettingActivity extends OrangeLifeBaseActivity implements
        OnClickListener {
    /** 设置标题 */
    private TextView tvSettingTitle, tv_title_score;
    /** 设置内容 */
    private RelativeLayout rlCleanCache, rlCheckUpdate, rlCopyRightDesc, rlShare;
    private LinearLayout ll_score;
    /** 消息提醒设置——显示打开或者关闭 */
    private ImageView ivNewsSetting, ivRight;
    private TextView tvNewVersionFlag;
    private String remark = "";
    private CleanCacheUtils clean = CleanCacheUtils.getInstance();
    private MyService.MyBinder myBinder;
    private boolean isBound;
    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myBinder = (MyService.MyBinder) service;
            UpdateBiz.requestUpdateVersion(myBinder, SettingActivity.this, 3);
        }
    };

    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_individual_setting);
        initView();
        findView();
    }

    /** 初始化组件 */
    private void initView() {
        tvSettingTitle = (TextView) findViewById(R.id.tv_title);
        ivNewsSetting = (ImageView) findViewById(R.id.iv_news_setting);
        ivRight = (ImageView) findViewById(R.id.iv_title_right);
        tvNewVersionFlag = (TextView) findViewById(R.id.tvNewVersionFlag);
        rlCleanCache = (RelativeLayout) findViewById(R.id.rl_clean_cache);
        rlCheckUpdate = (RelativeLayout) findViewById(R.id.rl_check_update);
        tv_title_score = (TextView) findViewById(R.id.tv_title_score);
        rlCopyRightDesc = (RelativeLayout) findViewById(R.id.rl_copyright_desc);
        rlShare = (RelativeLayout) findViewById(R.id.rl_share);
        ll_score = (LinearLayout) findViewById(R.id.ll_score);
        ll_score.setEnabled(true);
        ivRight.setVisibility(View.GONE);
        initJPush();
    }

    /** 实例化组件 */
    private void findView() {
        tvSettingTitle.setText(getResources().getString(R.string.setting));
        if (AppSp.getHasNewVersion()) {
            tvNewVersionFlag.setVisibility(View.VISIBLE);
        }
        tv_title_score.setText(UserSp.getPoint());
        ivNewsSetting.setOnClickListener(this);
        rlCheckUpdate.setOnClickListener(this);
        rlCopyRightDesc.setOnClickListener(this);
        rlCleanCache.setOnClickListener(this);
        rlShare.setOnClickListener(this);
    }

    private void initJPush() {
        if (AppSp.isOpenJPush()) {
            ivNewsSetting.setImageResource(R.drawable.turn_on);
        } else {
            ivNewsSetting.setImageResource(R.drawable.turn_off);
        }
    }

    private void setJPush() {
        AppSp.setIsOpenJPush(!AppSp.isOpenJPush());
        if (AppSp.isOpenJPush()) {
            ivNewsSetting.setImageResource(R.drawable.turn_on);
            JPushHelper.resumePush(getApplicationContext());
        } else {
            ivNewsSetting.setImageResource(R.drawable.turn_off);
            JPushHelper.stopPush(getApplicationContext());
        }
    }

    @Override
    public void onClick(View v) {
        /* 自定义dialog样式 */

        switch (v.getId()) {
            /** 判断消息提醒的状态并进行修改 0关闭  1开启*/
            case R.id.iv_news_setting:
                setJPush();
                break;
            /** 检查更新 */
            case R.id.rl_check_update:
                tvNewVersionFlag.setVisibility(View.INVISIBLE);
                if (isBound == false) {
                    bindService();
                } else {
                    UpdateBiz.requestUpdateVersion(myBinder, SettingActivity.this, 3);
                }
                break;
            /** 版本说明 */
            case R.id.rl_copyright_desc:
                showVersionInfoDialog();
                break;
            /** 清除缓存 */
            case R.id.rl_clean_cache:
                cleanCache();
                break;
            case R.id.rl_share:
                IntentHelper.getInstances().getIntent(this, ShareFriendActivity.class);
                break;
            default:
                break;
        }
    }

    private void bindService() {
        Intent intent = new Intent(SettingActivity.this, MyService.class);
        isBound = bindService(intent, connection, BIND_AUTO_CREATE);
    }

    private void showVersionInfoDialog() {
        try {
            final NewConfirmDialog dialog1 = new NewConfirmDialog(SettingActivity.this, " V" + SystemInfoUtil.getVersionName(this), AppSp.getVersionInfo(), "确认");
            dialog1.setClicklistener(new NewConfirmDialog.ClickListenerInterface() {
                @Override
                public void doLeft() {
                }

                @Override
                public void doRight() {
                    dialog1.dismiss();
                }
            });
            dialog1.show();
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showVersionInfoDialog1() {
        try {
            new AlertDialog.Builder(SettingActivity.this)
                    .setTitle(" V" + SystemInfoUtil.getVersionName(this))
                    .setMessage(AppSp.getVersionInfo())
                    .setPositiveButton(
                            getResources().getString(R.string.sure),
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(
                                        DialogInterface arg0, int arg1) {
                                }
                            }).show();
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void cleanCache() {
        final ConfirmDialog dialog1 = new ConfirmDialog(SettingActivity.this, "温馨提示", getResources().getString(R.string.is_clear_the_cache), "取消", "确认");
        dialog1.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                dialog1.dismiss();
            }

            @Override
            public void doRight() {
                dialog1.dismiss();
                dialog = DialogHelper.getInstance().createLoadingDialog(
                        SettingActivity.this,
                        getResources().getString(
                                R.string.clearing_the_cache));
                dialog.show();
                clean.init(SettingActivity.this);
                String path = Constant.FOLDER_PATH2;
                File file = new File(path);
                String filename = UserSp.getRegPhone()
                        + ".txt";
                FileHelper f = new FileHelper(
                        SettingActivity.this, filename);
                if (f.clearFile() && dialog != null) {
                    dialog.dismiss();
                }
                f.clearFile();
                f.deleteFile(filename);
                //ShoppingCartDao.getInstance().deleteAllGoodList();
                ToastHelper.getInstance()
                        .displayToastWithQuickClose(
                                getResources().getString(
                                        R.string.clear_finish));
            }
        });

        dialog1.show();
    }

    private void cleanCache1() {
        new AlertDialog.Builder(SettingActivity.this)
                .setTitle(getResources().getString(R.string.is_clear_the_cache))
                .setMessage(remark)
                .setPositiveButton(getResources().getString(R.string.sure),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
                                dialog = DialogHelper.getInstance().createLoadingDialog(
                                        SettingActivity.this,
                                        getResources().getString(
                                                R.string.clearing_the_cache));
                                dialog.show();
                                clean.init(SettingActivity.this);
                                String path = Constant.FOLDER_PATH2;
                                File file = new File(path);
                                String filename = UserSp.getRegPhone()
                                        + ".txt";
                                FileHelper f = new FileHelper(
                                        SettingActivity.this, filename);
                                if (f.clearFile() && dialog != null) {
                                    dialog.dismiss();
                                }
                                f.clearFile();
                                f.deleteFile(filename);
                                ShoppingCartDao.getInstance().deleteAllGoodList();
                                ToastHelper.getInstance()
                                        .displayToastWithQuickClose(
                                                getResources().getString(
                                                        R.string.clear_finish));
                            }
                        })
                .setNegativeButton(getResources().getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {

                            }
                        }).show();
    }

    @Override
    protected void onDestroy() {
        if (isBound) {
            unbindService(connection);
        }

        super.onDestroy();
    }
}
